﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework;
using CNVP.Framework.Helper;
using System.Data;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class User_Select_Window : AdminPage
    {
        BLL.CNVP_Users bll = new BLL.CNVP_Users();
        public int rplistCount = 0;
        int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                UserLevelBind();
                StartLoad(PageIndex);
                Page.Title = "选择会员账户";
            }

        }
        private void UserLevelBind()
        {
            IList<Model.CNVP_UserLevel> model_UserLevel = new BLL.CNVP_UserLevel().GetAll_CNVP_UserLevel();
            foreach (Model.CNVP_UserLevel m in model_UserLevel)
            {
                ddlUserLevel.Items.Add(new ListItem(m.LevelName, m.ID.ToString()));
            }
        }
        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dt = null;
            SQLConditionInfo[] st = new SQLConditionInfo[4];
            st[0] = new SQLConditionInfo("@UserLevelID", ddlUserLevel.SelectedValue);
            st[1] = new SQLConditionInfo("@NewsTitle", Public.FilterSql(txtkey.Text.Trim()));
            st[2] = new SQLConditionInfo("@NewsTitle1", Public.FilterSql(txtkey1.Text.Trim()));
            st[3] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);
            dt = bll.GetPage(this.GetType().Name, PageIndex, 8, out i, out j, st);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    rplistCount = dt.Rows.Count;//行数
                    //----------------------------------------添加列------------------------------------------------
                    dt.Columns.Add("oID", typeof(string));
                    dt.Columns.Add("oUserName", typeof(string));
                    dt.Columns.Add("oUserLevelID", typeof(string));
                    dt.Columns.Add("oDisCountPercent", typeof(string));
                    //----------------------------------------添加列结束--------------------------------------------
                    for (int k = 0; dt.Rows.Count > k; k++)
                    {
                        dt.Rows[k]["oID"] = dt.Rows[k]["ID"].ToString();
                        dt.Rows[k]["oUserName"] = dt.Rows[k]["UserName"].ToString();
                        string _LevelName="";
                        decimal _DisCountPercent = 0; 
                        var model_UserLevel= new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(int.Parse(dt.Rows[k]["UserLevelID"].ToString()));
                        if (model_UserLevel != null)
                        {
                            _LevelName = model_UserLevel.LevelName;
                            _DisCountPercent = model_UserLevel.DisCountPercent;
                        }
                        dt.Rows[k]["oUserLevelID"] = _LevelName;
                        dt.Rows[k]["oDisCountPercent"] = _DisCountPercent.ToString();
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            Label lbUserName = (Label)e.Item.FindControl("lbUserName");
            Label lbDisCountPercent = (Label)e.Item.FindControl("lbDisCountPercent");
            if (e.CommandName == "Select")
            {
                BasePage.CloseWindowReturnValues(id.ToString() + "*" + lbUserName.Text + "*" + lbDisCountPercent.Text);
            }
        }

        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex);

        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["SearchType"] = "0";
            StartLoad(PageIndex);
        }
    }
}